<?php

/* @author      :   THUYNT
 * @name        :   DetailController
 * @copyright   :   Fpt Online
 * @todo        :   Detail Controller
 */
class Euro2012_DetailController extends Zend_Controller_Action
{

    /**
     * init of controller
     */
    public function init()
    {
        //Disable layout
        $this->_helper->layout->setLayout('layoutdetail');
        
        //Assign metadata
        $this->view->headTitle()->prepend(META_TITLE);
        $this->view->headMeta()->prependName('keywords',META_KEYWORDS);
        $this->view->headMeta()->prependName('description',META_DESC);
    }

    //index action
    public function indexAction()
    {

       $intArticleId = (int) $this->getRequest()->getParam('id', 0);
        if ($intArticleId <= 0 || $intArticleId > 5000000) {
            $this->_redirect('/error');
            return;
        }
        // Get page
        $intPage = (int) $this->getRequest()->getParam('page', 1);
        if ($intPage < 1 || $intPage > 5000000) {
            $this->_redirect('/error');
            return;
        }

        // Get article detail
        $instanceNews = News::getInstance();

        // View page 2, or 3
        if ($intPage > 1) {
            $arrArticleDetail = $instanceNews->getDetailPageExtend($intArticleId, $intPage);
        } else {
            $arrArticleDetail = $instanceNews->getDetailArticleByIDs(array($intArticleId));

            // Check active or existing article
            if (empty($arrArticleDetail) || empty($arrArticleDetail[$intArticleId])) {
                $this->_redirect('/error');
                return;
            }
            $arrArticleDetail = $arrArticleDetail[$intArticleId];
        }

        // Set to view
        $this->view->arrArticleDetail = $arrArticleDetail;

        // Set category info
        $this->view->categoryID = $arrArticleDetail['category_id'];

        // Get parent info
        $arrCateInfo = Category::getInstance()->getDetailbyCateId($arrArticleDetail['category_id']);
        if (!empty($arrCateInfo) && SITE_ID != $arrCateInfo['parent_id']) {
            $this->view->parentCategoryID = $arrCateInfo['parent_id'];
        }

        // Get article the same topic
        $arrTopic = $instanceNews->getTopicsByArticleID($intArticleId);
        //default params
        $arrArticles = $arrMediaResult = $arrTags = $arrArticleID = array();

        if (!empty($arrTopic)) {
            //loop to get arrid only
            foreach ($arrTopic as $topicDetail) {
                $topicID = intval($topicDetail['topic_id']);
                if ($topicID > 0) {
                    //push array topic id to get detail later
                    $topicIDs[] = $topicID;
                    //if row=null => default get LIMIT_TOPIC item
                    $topicDetail['row'] = intval($topicDetail['row']);
                    $topicDetail['row'] = ($topicDetail['row'] > 0) ? $topicDetail['row'] : LIMIT_TOPIC;
                    // Get list article in topic by topicid & row config
                    $arrArticle = $instanceNews->getListArticleByTopic($topicID, $topicDetail['row']);
                    if (!empty($arrArticle['data'])) {
                        //excluse current articleID
                        $arrData = array_diff($arrArticle['data'], array($intArticleId));
                        if (!empty($arrData)) {
                            $arrMediaResult[] = array('topic_id' => $topicID, 'data' => $arrData);
                            // Collect article id to get one time
                            $arrArticleID = array_merge($arrArticleID, $arrArticle['data']);
                        }
                    }
                }
            }
            // Get topic info
            $arrTopicDetail = $instanceNews->getDetailTopic($topicIDs);
            if(!empty($arrTopicDetail))
            {
                $this->view->arrTopicDetail = $arrTopicDetail;
            }
            // Unique article id
            $arrArticleID = array_slice(array_unique($arrArticleID),0,5);

            // Get article info
            $arrArticles = $instanceNews->getDetailArticleByIDs($arrArticleID);

        }

        // Get last news
        $arrPamram = array(
            'category_id' => $arrArticleDetail['category_id'],
            'todate'    => 0,
            'fromdate'  => $arrArticleDetail['publish_time'],
            'limit'     => LIMIT_OTHER_NEWS,
            'offset'    => 0,
            'intArticleId' => array($intArticleId)
        );

        //Result other news
        $arrNewsArticle = array();
        $arrNewsArticleID = $instanceNews->getLatestNewsOnCate($arrArticleDetail['category_id'], 0, LIMIT_OTHER_NEWS);
        if(!empty($arrNewsArticleID['data'])){
            $arrNewsArticle = $instanceNews->getDetailArticleByIDs($arrNewsArticleID['data']);
        }

        // Assign to view
        $this->view->arrRelativeArticle = $arrArticles;
        $this->view->arrTopic = $arrMediaResult;
        $this->view->arrNewsArticle = $arrNewsArticle;

        // Get tags
        $arrTagID = $instanceNews->getTagsByArticleID($intArticleId);

        $arrKeyword = array();

        if (!empty($arrTagID)) {
            $arrTags = $instanceNews->getTagByID(join(',', $arrTagID));
            foreach ($arrTags as $arrTag) {
                $arrKeyword[] = trim($arrTag['tag_name']);
            }
        }
        //$arrTags = array('tag_id' => 1, 'tag_name' => 'MU');
        $this->view->arrTags = $arrTags;

        // Get list reference
        $arrRefernce = array();
        if (!empty($arrArticleDetail['list_reference'])) {
            $arrRefernce = $instanceNews->getDetailArticleByIDs(explode(',', $arrArticleDetail['list_reference']));
        }
        $this->view->arrRefernce = $arrRefernce;

        //begin get list photo by tags
        $arrPhotoByTags = array();
        if ($intArticleId > 0)
        {
            //get list tag by article
            $arrTag = $instanceNews->getTagsByArticleID($intArticleId);
            if (!empty($arrTag))
            {
                $totalTag = count($arrTag);
                for ($i = 0; $i < $totalTag; $i++)
                {
                    //get list article by tag
                    $arrPhoto = $instanceNews->getListArticleByTag($arrTag[$i], LIMIT_RELATED_PHOTO, 0, ALBUM);

                    if ($arrPhoto["total"] > 0)
                    {
                        $arrPhotoByTags = array_merge($arrPhotoByTags, $arrPhoto["data"]);
                    }
                }
            }
            //get article detail list
            if (!empty($arrPhotoByTags))
            {
                $arrPhotoByTags   = array_splice($arrPhotoByTags, 0, LIMIT_RELATED_PHOTO);
                // get detail
                $arrPhotoByTags   = $instanceNews->getDetailArticleByIDs($arrPhotoByTags);
            }
        }
        //assign data
        $this->view->arrPhotoByTags = $arrPhotoByTags;

        // SEO
        $this->view->headTitle($arrArticleDetail['title'] . ' - VnExpress');
        $this->view->headMeta()->setName('description', $arrArticleDetail['lead']);
        $this->view->headMeta()->setName('keywords', join(',', $arrKeyword));
    }
}
?>
